<script type="text/javascript">
    $(document).ready(function(){
        $('#edit_hot_product_form').dialog({
            'autoOpen':false,
            'width':600,
            'height':438,
            'resizable':false,
            'modal':true,
            'title': 'Change hot image',
            'dialogClass':'fixed-dialog',
        });
        $('.list_hot_product').on('click','.edit_product_image',function(){
            var productUri = $(this).closest('tr').attr('val');
            $.post('~product/loadImageOfProduct',{productUri : productUri},function(data){
                $('#edit_hot_product_form').html(data);
                $('#edit_hot_product_form').dialog('open');
                $('.image_tool').css('right','-200px');
                $('.image_tool').css('display','block');
            });
        }); 
        $('.list_hot_product').on('click','.remove_product',function(){
            var productUri = $(this).closest('tr').attr('val');
            var dis = $(this);
            $.post('~product/deleteProduct',{productUri : productUri},function(data){
                if(data.indexOf('FALSE') == -1){
                    $.alertBox('Message',"Xóa thành công!");
                    dis.closest('tr').fadeOut(500,function(){
                        $(this).remove(); 
                    });
                }
            });
        }); 

    });
</script>

<div class="admin_header_title">MY PRODUCTS</div>
<div class="metro_table green_metro_style">
    <div class="metro_table_title"><i class="fa fa-bars"></i> List Products</div>
    <div class="metro_table_content">
        <div style="padding: 20px;">
            <div class="metro_page_number">
                <a class="flat_metro_button green_metro_style" href="<?php echo URL::getLink('product/dangsp'); ?>">Add a New Product <i class="fa fa-plus"></i></a>
                <ul>
                    <?php
                        $iBegin = ($template->get('page') <= 5 ? 0 : $template->get('page') - 5);
                        echo '<li '.($template->get('page') == 1 ? 'class="page_disable"' : '').' ><a '.($template->get('page') > 1 ? 'href="?page='.($template->get('page')-1).'"' : '').' ><i class="fa fa-long-arrow-left"></i> Prev</a></li>';
                        $i = 0;
                        for($i = $iBegin; $i<= $template->get('numberPage'); $i++)
                        {
                            echo '<li '.($i + 1 == $template->get('page') ? 'class="page_active" ' : '').'><a href="?page='.($i + 1).'">'.($i + 1).'</a></li>';
                            if($i - $iBegin == 4)
                                break;
                        }
                        $i +=1;
                        echo '<li '.($template->get('page') >= $template->get('numberPage') ? 'class="page_disable"' : '').' ><a '.($i < $template->get('numberPage') ? 'href="?page='.($i + 1).'"' : '').'>Next <i class="fa fa-long-arrow-right"></i></a></li>';
                    ?>

                </ul>
            </div>
            <table id="table_hot_product" class="list_hot_product">
                <thead>
                    <tr>
                        <th style="width: 60px;">Image</th>
                        <th style="width: 100px;">Product ID</th>
                        <th style="width: 100px;">Name</th>
                        <th style="width: 80px;">Time</th>
                        <?php if(database::isAdmin()){ echo '<th>OwnUser</th>';} ?>
                        <th style="width:80px;">Category</th>
                        <th style="width:300px;">Description</th>
                        <th style="width:100px;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        if($template->get('aProducts'))
                        {
                            foreach($template->get('aProducts') as $aProduct)
                            {
                                $aParams = explode('/',$aProduct['uri']);
                                echo '<tr val="'.$aProduct['uri'].'">';
                                echo '<td class="avatar_cell"><img class="not_show edit_product_image" src="'.URL::getLink('upload/'.(isset($aProduct['image']) ? $aProduct['image'] : $aProduct['defaultImage'])).'"></td>';
                                echo '<td><a href="'.$aProduct['uri'].'">'.end($aParams).'</a></td>';
                                echo '<td><a href="'.$aProduct['uri'].'">'.$aProduct['name'].'</a></td>';
                                echo '<td>'.date('d/m/Y' , $aProduct['time']).'</td>';
                                if(database::isAdmin())
                                {
                                    echo '<td><a href="'.$aProduct['userUri'].'">'.$aProduct['userName'].'</a></td>';
                                }
                                echo '<td><a href="'.$aProduct['categoryUri'].'">'.$aProduct['categoryName'].'</a></td>';
                                echo '<td class="description_cell"><div>'.$aProduct['description'].'</div></td>';
                                echo '<td><a href="'.URL::getLink('product/edit/'.end($aParams)).'" class="table_bt_metro_edit edit_hot_product"><i class="fa fa-pencil"></i></a><button class="table_bt_metro_delete remove_product"><i class="fa fa-trash-o"></i></button></td>';
                                echo '</tr>';
                            }
                        }
                    ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div id="edit_hot_product_form">

</div>